package org.reliablesource.prism.ui.composite;

/**
 *
 *  Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 *
 *	Created on: Mar 11, 2006
 *  @author Dennis Park <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.reliablesource.prism.ui.plugin.PrismPlugin;

public class ImageComposite extends Composite {

	public String CONTEXT_ID = "ImageComposite";
	private Display display = Display.getDefault();

	private ImageDescriptor imgDesc;
	
	private Canvas imageCanvas;

	private ToolBar toolbar;
	private ToolItem fit;
	private Button backButton;
	private Button forwardButton;
	private Label imageCount;
	
	public void registerContext(String contextPrefix){
		String context  = contextPrefix+"."+CONTEXT_ID;
		PrismPlugin.getDefault().addWidget(context, this);
		PrismPlugin.getDefault().addWidget(context + ".imageCanvas", imageCanvas);
		PrismPlugin.getDefault().addWidget(context + ".Toolbar", toolbar);
		PrismPlugin.getDefault().addWidget(context + ".Toolbar.back", backButton);
		PrismPlugin.getDefault().addWidget(context + ".Toolbar.forward", forwardButton);
	}
	

	public ImageComposite(Composite parent, int style) {
		super(parent, style);
		toolbar = new ToolBar(this, SWT.TOP | SWT.FLAT);

		// set toolbar layout data to fill up horizontal row of its location.
		GridData tbLayout = new GridData();

		tbLayout.verticalAlignment = GridData.BEGINNING;
		tbLayout.grabExcessVerticalSpace = false;
		tbLayout.grabExcessHorizontalSpace = true;
		toolbar.setLayoutData(tbLayout);

		fit = new ToolItem(toolbar, SWT.PUSH);
		fit.setText("Fit to canvas");
		fit.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent selectionEvent) {
				if (imgDesc != null) {
					imageCanvas.setBounds(ImageComposite.this.getClientArea());
					imageCanvas.redraw();
				}
			}
		});

		this.setLayout(new GridLayout(3, true));

		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 3;

		ScrolledComposite sc = new ScrolledComposite(this, SWT.V_SCROLL
				| SWT.H_SCROLL);
		sc.setLayoutData(gd);

		gd = new GridData();
		gd.horizontalSpan = 1;
		gd.grabExcessHorizontalSpace = true;
		gd.grabExcessHorizontalSpace = false;
		backButton = new Button(this, SWT.LEFT);
		backButton.setLayoutData(gd);

		gd = new GridData();
		gd.horizontalSpan = 1;
		gd.grabExcessHorizontalSpace = true;
		gd.grabExcessHorizontalSpace = false;
		imageCount = new Label(this, SWT.NONE);
		imageCount.setLayoutData(gd);

		gd = new GridData();
		gd.horizontalSpan = 1;
		gd.grabExcessHorizontalSpace = true;
		gd.grabExcessHorizontalSpace = false;
		forwardButton = new Button(this, SWT.RIGHT);
		forwardButton.setLayoutData(gd);

		backButton.setText("back");
		backButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ViewMediator.getInstance().backward();
			}
		});

		forwardButton.setText("forward");
		forwardButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ViewMediator.getInstance().forward();
			}
		});

		sc.setLayout(new FillLayout());
		imageCanvas = new Canvas(sc, SWT.BORDER);

		sc.setContent(imageCanvas);
		try {
			imageCanvas.setBounds(display.getActiveShell().getBounds());
		} catch (NullPointerException npe) {
			imageCanvas.setBounds(display.getBounds());
		}

		init();
	}

	protected void init() {
		try {
			imageCanvas.addPaintListener(new PaintListener() {
				public void paintControl(PaintEvent e) {
					if (imgDesc != null) {
						final Image image = new Image(display, imgDesc.getImageData().scaledTo(
								imageCanvas.getSize().x,
								imageCanvas.getSize().y));
						e.gc.drawImage(image, 0, 0);
						image.dispose();
					}
				}
			});
		} catch (NullPointerException e) {
			e.printStackTrace();
		}

	}

	public void setImage(ImageDescriptor _desc, boolean _redraw){
		if (this.isDisposed())
			return;
		imgDesc = _desc;
		
		if(_redraw)
			imageCanvas.redraw();
	}


	/* (non-Javadoc)
	 * @see org.eclipse.swt.widgets.Widget#dispose()
	 */
	public void dispose() {
		ViewMediator.getInstance().unregisterPart(this);
		super.dispose();
	}		
}
